package gateway.internal.api.message;

import gateway.api.message.Context;
import gateway.api.message.Message;
import gateway.api.message.SnowflakeIdFactory;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-15
 */
public class DefaultMessage implements Message
{
	private long id;
	private byte[] byteArray;
	private Context context;

	public DefaultMessage()
	{
		this.id = SnowflakeIdFactory.getSingleSnowflakeId().nextId();
	}

	@Override
	public long id()
	{
		return id;
	}

	@Override
	public String body()
	{
		return new String(byteArray, StandardCharsets.UTF_8);
	}

	@Override
	public byte[] byteArray()
	{
		return byteArray;
	}

	@Override
	public Context context()
	{
		return context;
	}

	@Override
	public boolean equals(Object o)
	{
		if (this == o)
		{
			return true;
		}
		if (o == null || getClass() != o.getClass())
		{
			return false;
		}
		DefaultMessage that = (DefaultMessage) o;
		return id == that.id;
	}

	@Override
	public int hashCode()
	{
		return Objects.hash(id);
	}

	@Override
	public String toString()
	{
		return String.format("%s{id: %d}", this.getClass().getSimpleName(), id);
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public void setByteArray(byte[] byteArray)
	{
		this.byteArray = byteArray;
	}

	public void setContext(Context context)
	{
		this.context = context;
	}
}
